﻿'' GoogleCode - http://code.google.com/p/outlook-4-you/
'' GNU GPL v3 Licencing

Imports Microsoft.Office.Interop.Outlook
Imports System.Diagnostics
Imports System.IO

Public Class BuildAddIn '' This plugin will contain the automatic build system
#Region "Declarations"
    Dim codecs As BuildCodex
#Region "UI declarations"
    Private WithEvents cb As Office.CommandBar
    Private WithEvents cbbRequest As Office.CommandBarButton
    Private WithEvents cbbLeave As Office.CommandBarButton
    Private WithEvents cbbBuild As Office.CommandBarButton
    Private Const cb_name As String = "Autobuild"
#End Region
#Region "Inspector UI"
    Private WithEvents cbt As Office.CommandBar
    Private WithEvents tbbCheck As Office.CommandBarButton
#End Region
#Region "Variable"
    Private WithEvents objApt As Outlook.AppointmentItem
    Private WithEvents objMail As Outlook.MailItem
    Private WithEvents objInspector As Outlook.Inspectors
    Private WithEvents CalItems, InboxItems, BuildItems As Outlook.Items
    Private olCal, olMail, olBuilds, olTasks As Outlook.Folder
    Private cLeave, pLeave, fLeave As Double

    Public Shared Build As String()
    Public Shared timer As Timers.Timer
#End Region
#End Region

#Region "VSTO required"
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        olCal = Me.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        olMail = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        olBuilds = olMail.Parent.folders("Builds")
        olTasks = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderTasks)
        objInspector = Application.Inspectors

        timer.Interval = 60000
        timer.Enabled = False
        timer.AutoReset = True
        AddHandler timer.Elapsed, AddressOf build_timer
    End Sub
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub
#End Region

#Region "Outlook tasks"
    Public Shared Function Send_email(ByVal too As String, ByVal subj As String, ByVal body As String) As Boolean
        Dim request As Outlook.MailItem = Globals.BuildAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem)
        Try
            With request
                .Subject = subj
                .Body = body
                .To = too
                .Importance = OlImportance.olImportanceHigh
                .Send()
            End With
            Return True
        Catch ex As SystemException
            MsgBox("Could not send email, Sorry")
            Return False
        End Try
    End Function
#End Region

#Region "Build Specific code"
    Private Sub build_timer()
        ' Are we building?
        ' Yes - do nothing
        ' No - next build
    End Sub

    Private Sub Build_Check()
        BuildItems = olTasks.Items
        For Each item In BuildItems
            If (TypeOf item Is Outlook.TaskItem) Then
                Dim objTask = TryCast(item, Outlook.TaskItem)
                If objTask IsNot Nothing Then

                    If objTask.Subject.Contains("Build Request Accepted - ") Then
                        If objTask.Status = OlTaskStatus.olTaskInProgress Then
                            If objTask.PercentComplete = 5 Then
                                Dim req As Request = codecs.Decode_Task(objTask.Body)
                                Build(Build.Length) = req.GUID
                            End If
                        End If
                    End If

                End If
            End If
        Next
    End Sub ' Is a build ready to be started?

    Private Sub Build_Process(ByVal components As String()())
        ' Read components
        ' For i = 0 to components.length
        '   select case components(i)(1)
        '       case "sln"
        '           Build_VS_SLN(components(i)(0))
        '       case "batch"
        '           Build_VS_SLN(components(i)(0))
        '   End select
        ' Next
    End Sub

    Private Sub Local_Compile_C(ByVal program As String)
        Dim func As New Functional
        func.Commandline("gmake /f Makefile " & program & " > output.txt")
        Dim app_path As String = "C:\output.txt"
        If func.ErrorCheck(app_path, "error") = True Then
            MsgBox("Compilation error")
        End If
    End Sub ' Standard local compile

    Private Sub Build_VS_SLN(ByVal solution As String)
        Dim func As New Functional
        func.Commandline("msbuild " & solution & ".sln /t:" & solution & ":Rebuild")
    End Sub

    Private Sub Putty_Compile_C()
        Dim func As New Functional
        Dim app_path As String = "C:\"
        Dim bat_name As String = "Plink.bat"
        Dim cmd_name As String = "command.txt"
        Dim user As String = "bigdave@192.168.1.1"
        Dim pwd As String = "davebig"
        If func.CreateFile(bat_name, app_path) = True Then
            Using sw As New StreamWriter(app_path & bat_name)
                sw.WriteLine("plink -ssh -u" & user & " -p" & pwd & " -m " & app_path & cmd_name)
            End Using
        End If
        If func.CreateFile(cmd_name, app_path) = True Then
            Using sw As New StreamWriter(app_path & cmd_name)
                sw.WriteLine("cd /home")
                sw.WriteLine("gmake -f Makefile 1>output.txt 2>error.txt")
            End Using
        End If
        func.Commandline("Plink.bat")
    End Sub ' Use Plink to compile on remote UNIX box
#End Region

#Region "Miscellaneous"
    Private Sub WeekStarts(ByRef Start As Date, ByRef dEnd As Date)
        Select Case DateAndTime.Now.DayOfWeek
            Case DayOfWeek.Monday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(6)
            Case DayOfWeek.Tuesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 1, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(5)
            Case DayOfWeek.Wednesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 2, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(4)
            Case DayOfWeek.Thursday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 3, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(3)
            Case DayOfWeek.Friday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 4, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(2)
            Case DayOfWeek.Saturday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 5, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(1)
            Case DayOfWeek.Sunday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 6, 0, 0, 0)
                dEnd = DateAndTime.Now
        End Select
    End Sub
    Private Function MoveFile(ByVal File As String, ByVal From As String, ByVal Too As String) As Boolean
        If System.IO.File.Exists(From & File) = True Then
            Try
                System.IO.File.Move(From & File, Too & File)
            Catch ex As IO.IOException
                Return False
            End Try
            Return True
        End If
    End Function
    Private Function MoveDirectory(ByVal From As String, ByVal Too As String) As Boolean
        If System.IO.Directory.Exists(From) = True Then
            Try
                System.IO.Directory.Move(From, Too)
            Catch ex As IO.IOException
                Return False
            End Try
            Return True
        End If
    End Function
    Private Function CopyFile(ByVal File As String, ByVal From As String, ByVal Too As String) As Boolean
        If System.IO.File.Exists(From & File) = True Then
            Try
                System.IO.File.Copy(From & File, Too & File, True)
            Catch ex As IO.IOException
                Return False
            End Try
            Return True
        End If
    End Function
    Private Function CopyDirectory(ByVal From As String, ByVal Too As String) As Boolean
        If System.IO.Directory.Exists(From) = True Then
            Try
                My.Computer.FileSystem.CopyDirectory(From, Too, True)
            Catch ex As IO.IOException
                Return False
            End Try
            Return True
        End If
    End Function
    Private Function CopyDirectory(ByVal path As String) As Boolean
        If System.IO.Directory.Exists(path) = False Then
            Try
                System.IO.Directory.CreateDirectory(path)
            Catch ex As IO.IOException
                Return False
            End Try
            Return True
        End If
    End Function
#End Region
End Class